﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data;
using MySql.Data.MySqlClient;

/// <summary>
/// Descripción breve de PracticasDA
/// </summary>
public static class PracticasDA
{
    public static string insertPractica(string practicadesc, string practicacompleja, string practicacobertura)
    {
        string consulta = null;
        MySqlConnection conexion = Conexion.obtConexion();
        consulta = @"INSERT INTO practicas (practicadesc,practicacompleja,practicacobertura)
                    VALUES ('" + practicadesc + "','" + practicacompleja + "','" + practicacobertura + "')";
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        try
        {
            conexion.Open();
            comando.ExecuteReader();
            conexion.Close();
            return "Practica agregada correctamente";
        }
        catch
        {
            return consulta;
        }
    }

    public static string updatePractica(string practicaid, string practicadesc, string practicacompleja, string practicacobertura)
    {
        string consulta = null;
        MySqlConnection conexion = Conexion.obtConexion();
        consulta = @"UPDATE practicas
                     SET practicadesc='" + practicadesc +
                     "', practicacompleja='" + practicacompleja +
                     "', practicacobertura='" + practicacobertura +
                     "' WHERE practicaid=" + practicaid;
            
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        try
        {
            conexion.Open();
            comando.ExecuteReader();
            conexion.Close();
            return "Practica editada correctamente";
        }
        catch
        {
            return consulta;
        }
    }


    public static DataTable selectPractica(string practicaid)
    {
        MySqlConnection conexion = Conexion.obtConexion();
        DataTable tabla = new DataTable();
        string consulta = "SELECT * FROM practicas WHERE practicaid=" + practicaid;
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        conexion.Open();
        MySqlDataReader dataReader = comando.ExecuteReader();
        tabla.Load(dataReader, LoadOption.OverwriteChanges);
        conexion.Close();
        return tabla;
    }

    public static DataTable selectPracticas()
    {
        MySqlConnection conexion = Conexion.obtConexion();
        DataTable tabla = new DataTable();
        string consulta = "SELECT * FROM practicas ORDER BY practicadesc";
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        conexion.Open();
        MySqlDataReader dataReader = comando.ExecuteReader();
        tabla.Load(dataReader, LoadOption.OverwriteChanges);
        conexion.Close();
        return tabla;
    }

    public static string deletePractica(string practicaid)
    {
        MySqlConnection conexion = Conexion.obtConexion();
        string consulta = "DELETE FROM practicas WHERE practicaid=" + practicaid;
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        try
        {
            conexion.Open();
            comando.ExecuteReader();
            conexion.Close();
            return "Practica eliminada";
        }
        catch
        {
            return consulta;
        }
    }

    public static DataTable buscarPractica(string cadena)
    {
        MySqlConnection conexion = Conexion.obtConexion();
        DataTable tabla = new DataTable();
        string consulta = "SELECT * FROM practicas WHERE practicadesc like '%" + cadena + "%'";
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        conexion.Open();
        MySqlDataReader dataReader = comando.ExecuteReader();
        tabla.Load(dataReader, LoadOption.OverwriteChanges);
        conexion.Close();
        return tabla;
    }

    public static DataTable selectPracticasxPlan(string planid)
    {
        MySqlConnection conexion = Conexion.obtConexion();
        DataTable tabla = new DataTable("practicas");
        string consulta = @"SELECT *
                            FROM practicas NATURAL JOIN planes_practicas
                            WHERE planid = " + planid;
        MySqlCommand comando = new MySqlCommand(consulta, conexion);
        conexion.Open();
        MySqlDataReader dataReader = comando.ExecuteReader();
        tabla.Load(dataReader, LoadOption.OverwriteChanges);
        conexion.Close();
        return tabla;
    }
}
